package com.xiyan.mocklocation.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xiyan.mocklocation.domain.LoginBody;
import com.xiyan.mocklocation.domain.MainConfigBody;
import com.xiyan.mocklocation.domain.PostCardBody;
import com.xiyan.mocklocation.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.security.SecureRandom;
import java.util.Objects;

/**
 * @Description:
 * @Auther: wangjiao
 * @Date: 2021-07-12 11:32
 */
@Slf4j
@RestController
//@RequestMapping("/files")

public class MockController {
    @PostMapping("/login")
    public JsonResult login(@Validated @RequestBody LoginBody body) {
        log.info("登录:{}", JSONObject.toJSONString(body));
        JSONObject js = new JSONObject();

        return JsonResult.success(js);
    }

    @PostMapping("/upload_license")
    public JsonResult upload_license(MultipartFile avatar) {
        if (Objects.isNull(avatar)) {
            return JsonResult.error();
        }
        long size = avatar.getSize();
        String name = avatar.getOriginalFilename();
        log.info("文件名称：{}，大小：{}", name, size);
        if (size < 1) {

            return JsonResult.error();
        }
        return JsonResult.success(null);
    }

    @GetMapping("/main_config")
    public JsonResult main_config() {
        JSONObject js = new JSONObject();
        js.put("current", 0);
        js.put("serverIp0", "192.168.200.32");
        js.put("serverIp1", "192.168.200.35");
        js.put("serverIp2", "192.168.200.22");
        js.put("serverIp3", "192.168.200.92");
        js.put("serverPort0", "444");
        js.put("serverPort1", "444");
        js.put("serverPort2", "222");
        js.put("serverPort3", "444");
        return JsonResult.success(js);
    }

    @PostMapping("/main_config")
    public JsonResult mainConfig(@Validated @RequestBody MainConfigBody body) {
//        JSONObject js=new JSONObject();
//        js.put("current", 0);
//        js.put("serverIp0","192.168,200.32");
//        js.put("serverIp1","192.168,200.35");
//        js.put("serverIp2","192.168,200.22");
//        js.put("serverIp3","192.168,200.92");
//        js.put("serverPort0","444");
//        js.put("serverPort1","444");
//        js.put("serverPort2", "222");
//        js.put("serverPort3","444");
        return JsonResult.success(body);
    }

    @GetMapping("/mcard_status")
    public JsonResult mcard_status() {
        int count = 10;
        String version = "1.1.1";
        JSONArray groupAry = new JSONArray();
        SecureRandom secureRandom = new SecureRandom();
        for (int i = 1; i <= count; i++) {
            JSONObject g = new JSONObject();
            g.put("version", version);
            g.put("group", i);
            JSONArray cards = new JSONArray();
            for (int j = 1; j <= count; j++) {
                JSONObject c = new JSONObject();
                c.put("online", j%2==0?1:0);
                c.put("e", secureRandom.nextInt(100));
                c.put("cardid",String.valueOf( secureRandom.nextInt(999999999)));
                c.put("slot", j);
                cards.add(c);
            }
            g.put("cards", cards);
            groupAry.add(g);
        }
        return JsonResult.success(groupAry);
    }

    @PostMapping("/post_card")
    public JsonResult post_card(@Validated @RequestBody PostCardBody body) {
        return JsonResult.success(body);
    }

}
